<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #canvas{
      border: 1px solid black;
    }
  </style>
  <script>
    var ctx;
    var canvasW = 600
    var canvasH = 300
    var ball = {
      x: 100,
      y: 100,
      r: 25,
      dx: 5,
      dy: 2,
      g: 0.98,
      t: 0,
      lose: 1,
      flag: false,
      v: 0,
      stop: false,
      lastPos: 100,
      draw: function() {
        ctx.beginPath()
        ctx.fillStyle = 'blue'
        ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, true)
        ctx.fill()
      }
    }
    function draw() {
      ctx = document.getElementById('canvas').getContext('2d')
      ctx.fillStyle = 'rgba(255,255,255,0.3)';
      ctx.fillRect(0,0,canvasW,canvasH);
      ball.x += ball.dx
      ball.t++
      if (!ball.stop) {
        if (ball.flag) {
          var v = ball.v - ball.g * ball.t
          ball.y = ball.lastPos - (ball.v * ball.t - ball.g * Math.pow(ball.t, 2) / 2)
          if (v <= 0) {
            ball.flag = false
            ball.t = 0
            ball.lastPos = ball.y
          }
        } else {
          ball.v = ball.g * ball.t
          ball.y = ball.lastPos + ball.g * Math.pow(ball.t, 2) / 2
        }
      }
      
      if (ball.x + ball.r > canvasW || ball.x < ball.r) {
        ball.dx = -ball.dx
      }
      if (ball.y + ball.r >= canvasH) {
        ball.flag = true
        ball.t = 0
        ball.lastPos = ball.y
        ball.v -= ball.lose
        if (ball.v <= 0) {
          ball.stop = true
        } 
      }
      ball.draw()
      window.requestAnimationFrame(draw)
    }
  </script>
</head>
<body onload="draw()">
  <canvas id="canvas" width="600" height="300"></canvas>
</body>
</html>